package com.seckillpro.seckill_pro.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seckillpro.seckill_pro.mapper.TGoodsMapper;
import com.seckillpro.seckill_pro.mapper.TSecKillGoodsMapper;
import com.seckillpro.seckill_pro.pojo.TGoods;
import com.seckillpro.seckill_pro.pojo.TSecKillGoods;
import com.seckillpro.seckill_pro.service.ITGoodsService;
import com.seckillpro.seckill_pro.vo.AddGoodVo;
import com.seckillpro.seckill_pro.vo.GoodsVo;
import com.seckillpro.seckill_pro.vo.RespBean;
import com.seckillpro.seckill_pro.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyx
 * @since 2021-03-05
 */
@Service
public class TGoodsServiceImpl extends ServiceImpl<TGoodsMapper, TGoods> implements ITGoodsService {
    @Autowired
    private TGoodsMapper goodsMapper;

    @Autowired
    private TSecKillGoodsMapper secKillGoodsMapper;

    @Autowired
    private ITGoodsService goodsService;

    /**
     * 获取商品列表
     *
     * @return
     */
    @Override
    public List<GoodsVo> findGoodsVoList() {

        return goodsMapper.findGoodsVo();

    }

    /**
     * 获取商品详情
     *
     * @param goodId
     * @return
     */
    @Override
    public GoodsVo findGoodsVoByGoodId(Long goodId) {
        return goodsMapper.findGoodsVoByGoodId(goodId);
    }

    /**
     * 添加商品
     *
     * @param addGoodVo
     * @return
     */
    @Override
    public RespBean addGood(AddGoodVo addGoodVo) {
        Date endTime = null;
        Date startTime = null;
        if (ObjectUtil.isEmpty(addGoodVo)) {
            return RespBean.error(RespBeanEnum.GOOD_EMPTY);
        }
        //判断时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            startTime = sdf.parse(addGoodVo.getStartTime());
            endTime = sdf.parse(addGoodVo.getEndTime());
            if (endTime.compareTo(startTime) == -1) {
                return RespBean.error(RespBeanEnum.DATE_ERROR);
            }
        } catch (ParseException e) {
            System.out.println("格式错误");
        }


        TGoods goods = new TGoods();

        goods.setGoodDetail(addGoodVo.getGoodDetail());
        goods.setGoodImg("/imgs/" + addGoodVo.getNewName());
        goods.setGoodName(addGoodVo.getGoodName());
        goods.setGoodPrice(addGoodVo.getGoodOrgPrice());
        goods.setGoodTitle(addGoodVo.getGoodName());
        goods.setGoodStock(addGoodVo.getStock());
        goods.setId(new Random().nextLong() % 10000 * -1);
        //创建新商品
        int goodsIsert = goodsMapper.insert(goods);
        if (goodsIsert <= 0) {
            return RespBean.error(RespBeanEnum.GOOD_INSERT_ERROR);
        }

        TSecKillGoods secKillGoods = new TSecKillGoods();
        secKillGoods.setStockCount(addGoodVo.getSecKillAcc());
        secKillGoods.setEndDate(endTime);
        secKillGoods.setStartDate(startTime);
        secKillGoods.setSecKillPrice(addGoodVo.getGoodSecKillPrice());
        secKillGoods.setGoodId(goods.getId());

        int secKillGoodsIsert = secKillGoodsMapper.insert(secKillGoods);

        if (secKillGoodsIsert <= 0) {
            return RespBean.error(RespBeanEnum.SECKILL_GOOD_INSERT_ERROR);
        }

        return RespBean.success();
    }

    /**
     * @param goodId
     * @return
     */
    @Override
    public RespBean removeGood(Long goodId) {

        GoodsVo goodsVo = goodsService.findGoodsVoByGoodId(goodId);
        if (ObjectUtil.isEmpty(goodsVo)) {
            return RespBean.error(RespBeanEnum.GOOD_EMPTY);
        }

        goodsMapper.delete(new QueryWrapper<TGoods>().eq("id", goodId));
        secKillGoodsMapper.delete(new QueryWrapper<TSecKillGoods>().eq("good_id", goodId));
        return RespBean.success();
    }


    private static long uniqId() {
        Random random = null;
        String nanoRandom = System.nanoTime() + "" + random.nextInt(99999);
        int hash = Math.abs(UUID.randomUUID().hashCode());
        int needAdd = 19 - String.valueOf(hash).length() + 1;
        return Long.valueOf(hash + "" + nanoRandom.substring(needAdd));
    }
}
